package com.sxkj.web.controller.member;

import com.github.pagehelper.PageInfo;
import com.sxkj.common.response.ResponseResult;
import com.sxkj.service.redis.RedlockService;
import com.sxkj.web.runnable.DaemonThread;
import com.sxkj.web.runnable.RedissonRunnable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.CountDownLatch;


@Api(tags = "TestController", description = "测试会员接口")
@Controller
@RequestMapping("/test")
public class TestController {



    @Autowired
    private RedlockService redlockService;

    @ApiOperation(value = "根据主键查member信息")
    @GetMapping(value = "/test/{id}")
    public ResponseResult test(@PathVariable String id) {
        try {
            int countNum = 100;
            CountDownLatch countDownLatch = new CountDownLatch(countNum);
            RedissonClient redissonClient = redlockService.redissonClient;
            long begin = System.currentTimeMillis();

            for (int i = 0; i < countNum; i++) {
                RedissonRunnable redissonRunnable=new RedissonRunnable(countDownLatch,"子线程" + (i+100),redissonClient);
                Thread thread=new Thread(redissonRunnable);
//                thread.setDaemon(true); //设置守护线程
                thread.start(); //开始执行分进程
//                new Thread(redissonRunnable).start();
            }

            System.out.println("主线程阻塞,等待所有子线程执行完成");
            countDownLatch.await();
            System.out.println("所有线程执行完成!");
            long end = System.currentTimeMillis();
            System.out.println("----------所有线程执行完成!------时间花费："+(end -begin) +"ms");
            return ResponseResult.success();
        } catch (Exception e) {
            return ResponseResult.failure(e.toString());
        }
    }

    @ApiOperation(value = "根据主键查member信息")
    @GetMapping(value = "/test1/{id}")
    public ResponseResult test1(@PathVariable String id) {
        try {
            DaemonThread thread = new DaemonThread();
            thread.setDaemon(true);
            thread.start();
            Thread.sleep(2000);
            System.out.println("我离开thread对象也不再打印了，也就是停止了！");
            return ResponseResult.success();
        } catch (Exception e) {
            return ResponseResult.failure(e.toString());
        }
    }


}
